package com.soft.collateral.service;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.soft.collateral.entity.Contracts;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Service
public class ContractsPDFService {

    public byte[] generateContractPDF(Contracts contract) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try (PdfWriter writer = new PdfWriter(baos);
             PdfDocument pdf = new PdfDocument(writer);
             Document document = new Document(pdf)) {

            // 1. 使用7.2.4版本的字体加载方式（明确指定编码和嵌入策略）
            PdfFont font = PdfFontFactory.createFont(
                    "C:/Windows/Fonts/simhei.ttf",
                    PdfEncodings.IDENTITY_H,
                    PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED
            );

            // 2. 添加合同内容（合并方法避免未解析错误）
            addTitle(document, font, "押品交易合同书");
            addContractContent(document, font, contract);
        }

        return baos.toByteArray();
    }

    private void addTitle(Document document, PdfFont font, String titleText) {
        document.add(
                new Paragraph(titleText)
                        .setFont(font)
                        .setFontSize(18)
                        .setBold()
                        .setTextAlignment(TextAlignment.CENTER)
                        .setMarginBottom(20)
        );
    }

    private void addContractContent(Document document, PdfFont font, Contracts contract) {
        // 合同基本信息
        document.add(new Paragraph("合同编号: " + contract.getContractNumber()).setFont(font));
        document.add(new Paragraph("押品编号: " + contract.getCollateralId()).setFont(font));
        document.add(new Paragraph("卖出方: " + contract.getSeller()).setFont(font));
        document.add(new Paragraph("买入方: " + contract.getBuyers()).setFont(font));
        document.add(new Paragraph("合同类型: " + getContractTypeName(contract.getContractsType())).setFont(font));
        document.add(new Paragraph("交易内容: " +
                (contract.getContractsContent() != null ? contract.getContractsContent() : "无")).setFont(font));
        document.add(new Paragraph("生效日期: " + contract.getEffectiveDate()).setFont(font));
        document.add(new Paragraph("交易金额: " + String.format("%.2f", contract.getAmount()) + "元").setFont(font));
        document.add(new Paragraph("付款方式: " + getPaymentMethodName(contract.getPaymentMethod())).setFont(font));
        document.add(new Paragraph("签订时间: " + contract.getSigningDate()).setFont(font));

        // 签名区域（原addSignatureArea内容）
        document.add(new Paragraph("\n"));
        document.add(new Paragraph("双方签字确认").setFont(font).setBold());
        document.add(new Paragraph("卖出方签字: ___________________").setFont(font));
        document.add(new Paragraph("买入方签字: ___________________").setFont(font));
        document.add(new Paragraph("签订日期: " + contract.getSigningDate()).setFont(font));
    }

    private String getContractTypeName(Integer type) {
        return type == null ? "未知" : (type == 0 ? "正常售出" : "拍卖售出");
    }

    private String getPaymentMethodName(Integer method) {
        return method == null ? "未知" : (method == 0 ? "一次付清" : "分期付款");
    }
}